package com.hyyt.shopjsp.cms.controller;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hyyt.shopjsp.cms.pojo.CmsArticle;
import com.hyyt.shopjsp.cms.service.ICmsArticleAttachmentService;
import com.hyyt.shopjsp.cms.service.ICmsArticleService;
import com.hyyt.shopjsp.cms.service.ICmsCategoryService;
import com.hyyt.shopjsp.basic.pojo.BasicUsers;
import com.hyyt.shopjsp.basic.service.IBasicActorPurviewService;
import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;

/**
 * Copyright (c) 2017 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author Wang Youchun on 2017/03/09 0024下午 3:12.
 */
/**
 * 基本控制层操作类
 *
 * @author wangyouchun
 * @date 2017/3/9 14:47
 */
@Controller
@RequestMapping("/back/cmsArticle")
public class CmsArticleController extends BaseController{
	
	private Logger log = Logger.getLogger(CmsArticleController.class);
	private String basePath = "/back/cms/article/";
	@Resource
	public ICmsArticleService cmsArticleService;
	@Resource
	public ICmsCategoryService cmsCategoryService;
	@Resource
	public ICmsArticleAttachmentService cmsArticleAttachmentService;


	/**
	 * 根据id初始化文章列表
	 * @param param 参数
	 * @return 返回到指定页面
	 */
	@RequestMapping("gotoArticleList")
	public String gotoArticleList(Map<String, Object> param){
		//默认指定分类初始化右侧列表文章数据
		param.put("categoryId", "1");
		return basePath+"articleList";
	}
	
	/**
	 * 查询所有文章
	 * @param categoryId	 新闻所在的分类id
	 * @param title 新闻标题
	 * @param page
	 * @param response
	 */
	@RequestMapping("listArticle")
	@ResponseBody
	public void listArticle(Integer categoryId,String title,String isShow,Page page, HttpServletResponse response){
		Map<String, Object> param = new HashMap<>();
		if (Utils.objectIsNotEmpty(categoryId)) {
			param.put("categoryId", String.valueOf(categoryId));
		}else{
			param.put("categoryId", "1");
		}
		if (Utils.objectIsNotEmpty(title)) {
			param.put("title", title.trim());
		}
		if (Utils.objectIsNotEmpty(isShow)) {
			param.put("isShow", isShow);
		}
		int totalRecordCount=cmsArticleService.countCmsArticle(param);
		page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
		//关联查询文章内容及分类名
		List<CmsArticle> list=cmsArticleService.queryArticleOrCategory(param, page.getPageRecordBeginIndex(), page.getPageSize());
		param.clear();
		param.put("total", totalRecordCount);// total键 存放总记录数，必须的
        param.put("rows", list);// rows键 存放每页记录 list
        ResultMsg.create(param, response);  //把内容发送到页面
	}

	/**
	 * 根据id获得文章对象
	 * @param articleId
	 * @return
	 */
	@RequestMapping(value="getCmsArticle",method=RequestMethod.POST)
	@ResponseBody
	public ResultMsg getCmsArticle(Integer articleId){
		if(Utils.objectIsNotEmpty(articleId)){
			CmsArticle cmsArticle =cmsArticleService.loadCmsArticle(articleId);
			return ResultMsg.create(true, null, FastJsonUtils.toJSONString(cmsArticle));
		}
		return ResultMsg.create(false);
	}

	/**
     * 删除传入的文章id
     *
     * @param articleId
     */
    @RequestMapping(value = "deleteCmsArticle", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg deleteCmsArticle(String articleId) {
        if (Utils.objectIsNotEmpty(articleId)) {
        	cmsArticleService.deleteCmsArticle(Integer.parseInt(articleId));
        	return ResultMsg.create(true);
        }else{
			return ResultMsg.create(false);
		}
    }

    /**
	 * 保存或修改文章
	 * @param cmsArticle
	 * @return
	 */
	@RequestMapping("saveOrUpdateCmsArticle")
	@ResponseBody
	public ResultMsg saveOrUpdateCmsArticle(CmsArticle cmsArticle){
		String usersName=getSessionUsersName();
		if(Utils.objectIsNotEmpty(cmsArticle)){
			cmsArticle.setAuthor(usersName);
			if(Utils.objectIsNotEmpty(cmsArticle.getArticleId())){
				cmsArticle.setUpdateTime(new Timestamp(System.currentTimeMillis()));
				cmsArticle.setPublishUser(usersName);
				cmsArticleService.updateCmsArticle(cmsArticle);
			}else{
				cmsArticle.setCreateTime(new Timestamp(System.currentTimeMillis()));
				cmsArticle.setUpdateTime(new Timestamp(System.currentTimeMillis()));
				cmsArticle.setModifyUser(usersName);
				if(Utils.objectIsEmpty(cmsArticle.getCategoryId())){
					cmsArticle.setCategoryId(1);
				}
				cmsArticleService.saveCmsArticle(cmsArticle);
			}
			return ResultMsg.create(true);
		}else{
			return ResultMsg.create(false);
		}

	}
}
